package com.melody.android

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.melody.android.databinding.ActivityMainBinding
import com.melody.android.logic.model.Song
import com.melody.android.logic.util.MediaPlayer
import com.melody.android.logic.util.getSelectedSong
import com.melody.android.ui.activity.BaseActivity
import com.melody.android.ui.activity.SongActivity
import com.melody.android.ui.fragment.DiscoveryFragment
import com.melody.android.ui.fragment.MineFragment
import com.melody.android.ui.fragment.SongFragment
import com.melody.android.ui.viewModel.SongViewModel

class MainActivity : BaseActivity() {

    private lateinit var binding: ActivityMainBinding                   // 声明视图绑定变量
    private lateinit var songViewModel: SongViewModel
    private lateinit var selectedSong: Song                             // 当前选中的歌曲
    private lateinit var bottomNavigationView: BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 初始化SongViewModel
        songViewModel = ViewModelProvider(this)[SongViewModel::class.java]
        // (响应)更新歌曲信息视图
        songViewModel.selectedSong.observe(this) { song ->
            selectedSong = song
            updateSongInfoView(song)
        }

        // 点击歌曲音乐信息跳转到音乐播放详情页面
        binding.clSongInfo.setOnClickListener {
            val intent = Intent(this, SongActivity::class.java)
            intent.putExtra("selectedSong", selectedSong)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()

        initView()
        // SongListActivity选中歌曲返回后更新SongInfoView
        if (MediaPlayer.getPlaybackState()) {
            selectedSong = getSelectedSong(MelodyApplication.context)
            updateSongInfoView(selectedSong)
        }
    }

    private fun initView() {
        binding.imgPlayState.setImageResource(if (MediaPlayer.getPlaybackState()) R.drawable.ic_pause_circle else R.drawable.ic_play_circle)
        initBottomNavigationView()
    }

    /**
     * 初始化BottomNavigationView
     */
    private fun initBottomNavigationView() {
        bottomNavigationView = binding.bottomNavigationView

        // 默认显示DiscoveryFragment
        replaceFragment(DiscoveryFragment())
        bottomNavigationView.setOnItemSelectedListener { menuItem ->
            val fragment = when (menuItem.itemId) {
                R.id.menu_item_discovery -> DiscoveryFragment()
                R.id.menu_item_song -> SongFragment()
                R.id.menu_item_mine -> MineFragment()
                else -> null
            }
            fragment?.let { replaceFragment(it) }
            return@setOnItemSelectedListener true
        }
    }

    /**
     * 切换Fragment
     * @param fragment 待切换的Fragment实例
     */
    private fun replaceFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, fragment)
            .commit()
    }

    /**
     * 更新歌曲信息视图(MainActivity, 当前正在播放的歌曲)
     * @param song 歌曲实例
     */
    private fun updateSongInfoView(song: Song) {
        binding.clSongInfo.visibility = View.VISIBLE
        binding.imgSongCover.setImageResource(song.cover)
        binding.txtSongName.text = song.name
        binding.txtSongAuthor.text = song.author
        binding.imgPlayState.setImageResource(R.drawable.ic_pause_circle)

        binding.imgPlayState.setOnClickListener {
            binding.imgPlayState.setImageResource(if (MediaPlayer.getPlaybackState()) R.drawable.ic_play_circle else R.drawable.ic_pause_circle)
            MediaPlayer.pauseSong()
        }
    }

}